let code = '';
let lastTime, nextTime;
let lastCode, nextCode;
function scanEvent(e, cb) {
  if (window.event) { // IE
    nextCode = e.keyCode;
   } else if (e.which) { // Netscape/Firefox/Opera
    nextCode = e.which;
   }
   if (nextCode === 13) {
    if (code.length < 3) return; // 手动输入的时间不会让code的长度大于2，所以这里只会对扫码枪有
    console.log(code,lastTime,lastCode,'123');

    cb(code) // 获取到扫码枪输入的内容，做别的操作
    code = '';
    lastCode = '';
    lastTime = '';
    nextTime=''
    return;
   }
   nextTime = new Date().getTime();
   if (!lastTime && !lastCode) {//第一个输入
    code += e.key;
   } 
   console.log(e.key,'llllllllll');
   if (lastCode && lastTime && nextTime - lastTime > 30) { // 当扫码前有keypress事件时,防止首字缺失
    code = e.key;
   } else if (lastCode && lastTime) {
    code += e.key;
   }
   lastCode = nextCode;
   lastTime = nextTime;
}

export { scanEvent };